%AHA tools for result displaying
%Developed by ruiguo email: ruiguo.ok#163.com

%Read ALL ROIs infomation
%save patiten info to the excel files

close all ;
clear all ;


T2_cbar = jet;
files_dir = 'H:\Projects\MST1\EXPData\Invivo\AllPatients\MOLLI53';
dirInfo = dir( files_dir );
avgSlices = 3;
T1_T2_w = 1;
%% save to excel
% open excel file
str = date ;
xlsxfilenm =[ '_Voluteers_LV_Septeum_BP_'  str '_T'  num2str(T1_T2_w) '.xlsx' ];
filepath = [ files_dir  xlsxfilenm];
try
    Excel=actxgetrunningserver('Excel.application');
catch
    Excel=actxserver('Excel.application');
end

Excel.visible=1;
Workbooks= Excel.Workbooks;
if exist(filepath,'file' )
    Workbook = Excel.Workbooks.Open(filepath);
else
    Workbook = Excel.Workbooks.Add;
    Workbook.SaveAs(filepath);
end
Sheets=Excel.Activeworkbook.Sheets;
Sheet1=Sheets.Item(1);
Sheet1.Activate;


Start_row =1;  % global setting for the excel output  A D G J
Start_col = 'A';
row_ix = Start_row ;
ActivesheetRange = get(Sheet1,'Range',[(Start_col) num2str(row_ix) ]);
set(ActivesheetRange, 'Value', 'id');
ActivesheetRange = get(Sheet1,'Range',[char(Start_col+1)  num2str(row_ix) ]);
set(ActivesheetRange, 'Value', 'subjectName');
ActivesheetRange = get(Sheet1,'Range',[char(Start_col+2)  num2str(row_ix) ]);
set(ActivesheetRange, 'Value', 'Slice#');
ActivesheetRange = get(Sheet1,'Range',[char(Start_col+3) num2str(row_ix) ]);
set(ActivesheetRange, 'Value', 'SliceMean');
ActivesheetRange = get(Sheet1,'Range',[char(Start_col+4) num2str(row_ix) ]);
set(ActivesheetRange, 'Value', 'SliceSTD');
ActivesheetRange = get(Sheet1,'Range',[char(Start_col+5) num2str(row_ix) ]);
set(ActivesheetRange, 'Value', 'SeptumMean');
ActivesheetRange = get(Sheet1,'Range',[char(Start_col+6)  num2str(row_ix) ]);
set(ActivesheetRange, 'Value', 'SeptumSTD');
ActivesheetRange = get(Sheet1,'Range',[char(Start_col+7) num2str(row_ix) ]);
set(ActivesheetRange, 'Value', 'BloodMean');
ActivesheetRange = get(Sheet1,'Range',[char(Start_col+8)  num2str(row_ix) ]);
set(ActivesheetRange, 'Value', 'BloodSTD');

ActivesheetRange = get(Sheet1,'Range',[char(Start_col+9)  num2str(row_ix) ]);
set(ActivesheetRange, 'Value', 'Slice CV%');

ActivesheetRange = get(Sheet1,'Range',[char(Start_col+10)  num2str(row_ix) ]);
set(ActivesheetRange, 'Value', 'Septal CV%');






     summaryRow =  8+length(dirInfo)*(avgSlices+2)+2;
     
        ActivesheetRange = get(Sheet1,'Range',[(Start_col) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'iD');
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+1)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'Imaging Date');

        %-------------------------------------------------------------------------%
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+3)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+4) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'Slice T1 AVG');
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+5) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'Slice T1 STD');
        
        
        %-------------------------------------------------------------------------%
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+6)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
        
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+7) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'Slice Std AVG');
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+8) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'Slice Std STD');
        
        %-------------------------------------------------------------------------%
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+9)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
          
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+10) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'Septum T1 AVG');
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+11)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'Septum T1 STD');
        
        %-------------------------------------------------------------------------%
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+12)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
          
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+13) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'Septum Std AVG');
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+14)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'Septum Std STD');
        
        
        %-------------------------------------------------------------------------%
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+15)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
         
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+16) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'BP T1 AVG');
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+17)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'BP T1 STD');
        
        %-------------------------------------------------------------------------%
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+18)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+19) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'BP Std AVG');
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+20)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'Bp Std AVG');
        
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+21)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+22) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'Slice CV AVG');
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+23) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', 'Septum CV AVG');












row_ix = row_ix+2;
for subj_ix = 3:length(dirInfo)
    subject_name = dirInfo(subj_ix).name;
    subjectInfo = dir([files_dir filesep subject_name filesep '*.mat'] );
    image_name =char( regexpi(subjectInfo.name,'.*?(?=.mat)','match'));
    file_dir = [ files_dir filesep subject_name filesep];
    %read ROI info ;
    subject_dir_info = dir( file_dir );
    ROI_state = 0;
    for ix = 3:length(subject_dir_info )
        ROI_dir_name = subject_dir_info(ix).name;
        if(~isempty( regexpi(ROI_dir_name,'(?<=ROIs_).*','match')))
            ROI_dir = [ file_dir ROI_dir_name ];
            ROI_state  = 1;
            break;
        end
    end
    targetSliceNum = [];
    if(ROI_state )
        min_slice_num  = 20;
        max_slice_num = 0;
        ROI_dir_info = dir(ROI_dir);
        for ix = 3:length( ROI_dir_info)
            ROI_name = ROI_dir_info(ix).name;
            
            if( ~isempty( regexpi(ROI_name,'RS\d\d.mat')) || ~isempty(regexpi(ROI_name,'RS\d.mat')))
                str_cell = regexpi(ROI_name,'[\d][\d]{0,}','match') ;
                targetSliceNum = [ targetSliceNum str2num(str_cell{1,1})];
            end
        end
    end
    
    
    Segmentation_tables= {1};
    AHAInfo = {1};
    Thickness_img = [];
    bp_Area = [];
    
    T1_T2_excel = [ 'T_1';'T_2'];
    T1_T2_str=[ '{T_1}';'{T_2}'];
    T1_T2_DISP = [2000;100];
    % image_name = 'Fit4ParaSlice12sCOSATIRT1T2Fitting_QIAOHUIYU_WIP_3DCOSATIRT1T2_CLEAR_6_1';    % no suffix
    % file_dir =  'D:\paper\CO-register T1 T2 mapping\Results\HealthyVolunteers\PAR REC\Parameters dat\QiaoHuiyu\';
    
    T1_T2 =T1_T2_w;
    
    
   mean_Slices_thickness = zeros(1, length( targetSliceNum));
   std_Slices_thickness = zeros(1, length( targetSliceNum));
   
   mean_Slices_septum =zeros(1, length( targetSliceNum));
   std_Slices_septum =zeros(1, length( targetSliceNum));
   
   mean_Slices_bp =zeros(1, length( targetSliceNum));
   std_Slices_bp =zeros(1, length( targetSliceNum));
   
   mean_Slices_septum_cv =zeros(1, length( targetSliceNum));
   std_Slices_septum_cv =zeros(1, length( targetSliceNum));
   
   mean_Slices_cv =zeros(1, length( targetSliceNum));
   std_Slices_cv=zeros(1, length( targetSliceNum));
   
   center_pos = [];
    for ix = 1:length( targetSliceNum)
        Obj_slice_num = targetSliceNum(ix);
        [rsegment_info,aha_info,Thickness_figure, o_bp_Area]=readsingleroiinfo( file_dir, image_name, Obj_slice_num,center_pos,T1_T2,ROI_dir_name);
        [SeptumThickness_figure]=readSeptumROI( file_dir, image_name, Obj_slice_num,center_pos,T1_T2,ROI_dir_name);
        [BloodThickness_figure]=readBloodROI( file_dir, image_name, Obj_slice_num,center_pos,T1_T2,ROI_dir_name);
        Thickness_figure(Thickness_figure>3000) = 0;
        SeptumThickness_figure( Thickness_figure>3000) = 0;
        SeptumThickness_figure(isnan(SeptumThickness_figure)) = 0;
        BloodThickness_figure(BloodThickness_figure>3000) = 0;
        BloodThickness_figure(isnan(BloodThickness_figure)) = 0;

        %export to the excel
    
        ActivesheetRange = get(Sheet1,'Range',[(Start_col) num2str(row_ix) ]);
        set(ActivesheetRange, 'Value', subj_ix-2);
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+1)  num2str(row_ix) ]);
        set(ActivesheetRange, 'Value', subject_name);
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+2)  num2str(row_ix) ]);
        set(ActivesheetRange, 'Value', Obj_slice_num);
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+3) num2str(row_ix) ]);
        set(ActivesheetRange, 'Value', mean(Thickness_figure(Thickness_figure>0)));
       
        mean_Slices_thickness(ix) = mean(Thickness_figure(Thickness_figure>0)); 
        std_Slices_thickness(ix) = std(Thickness_figure(Thickness_figure>0)); 
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+4) num2str(row_ix) ]);
        set(ActivesheetRange, 'Value', std(Thickness_figure(Thickness_figure>0)));
        
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+5) num2str(row_ix) ]);
        set(ActivesheetRange, 'Value', mean(SeptumThickness_figure(SeptumThickness_figure>0)));
        
        mean_Slices_septum(ix) = mean(SeptumThickness_figure(SeptumThickness_figure>0));       
        std_Slices_septum(ix) = std(SeptumThickness_figure(SeptumThickness_figure>0)); 
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+6)  num2str(row_ix) ]);
        set(ActivesheetRange, 'Value', std(SeptumThickness_figure(SeptumThickness_figure>0)));
        
        mean_Slices_septum_cv(ix) =std(SeptumThickness_figure(SeptumThickness_figure>0))/mean(SeptumThickness_figure(SeptumThickness_figure>0))*100;
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+7) num2str(row_ix) ]);
        set(ActivesheetRange, 'Value', mean(BloodThickness_figure(BloodThickness_figure>0)));
        
        mean_Slices_bp(ix) = mean(BloodThickness_figure(BloodThickness_figure>0)); 
        std_Slices_bp(ix) = std(BloodThickness_figure(BloodThickness_figure>0)); 
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+8)  num2str(row_ix) ]);
        set(ActivesheetRange, 'Value', std(BloodThickness_figure(BloodThickness_figure>0)));
        
        
        mean_Slices_cv(ix) =std(Thickness_figure(Thickness_figure>0))/mean(Thickness_figure(Thickness_figure>0))*100;
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+9)  num2str(row_ix) ]);
        set(ActivesheetRange, 'Value',  mean_Slices_cv(ix));
        
        mean_Sep_cv(ix) =std(SeptumThickness_figure(SeptumThickness_figure>0))/mean(SeptumThickness_figure(SeptumThickness_figure>0))*100;
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+10)  num2str(row_ix) ]);
        set(ActivesheetRange, 'Value',  mean_Sep_cv(ix));
        
        row_ix = row_ix+1;
    end
     row_ix = row_ix+1;
     
     summaryRow =  8+length(dirInfo)*(avgSlices+2)+subj_ix;
     
        ActivesheetRange = get(Sheet1,'Range',[(Start_col) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', subj_ix-2);
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+1)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', subject_name);

        %-------------------------------------------------------------------------%
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+3)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+4) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', mean(mean_Slices_thickness));
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+5) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', std(mean_Slices_thickness));
        
        
        %-------------------------------------------------------------------------%
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+6)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
        
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+7) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', mean(std_Slices_thickness));
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+8) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', std(std_Slices_thickness));
        
        %-------------------------------------------------------------------------%
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+9)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
          
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+10) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', mean(mean_Slices_septum));
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+11)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', std(mean_Slices_septum));
        
        %-------------------------------------------------------------------------%
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+12)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
          
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+13) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', mean(std_Slices_septum));
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+14)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', std(std_Slices_septum));
        
        
        %-------------------------------------------------------------------------%
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+15)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
         
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+16) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', mean(mean_Slices_bp));
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+17)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', std(mean_Slices_bp));
        
        %-------------------------------------------------------------------------%
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+18)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+19) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', mean(std_Slices_bp));
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+20)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', std(std_Slices_bp));
        
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+21)  num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', '');
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+22) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', mean(mean_Slices_cv));
        
        ActivesheetRange = get(Sheet1,'Range',[char(Start_col+23) num2str(summaryRow) ]);
        set(ActivesheetRange, 'Value', mean(mean_Slices_septum_cv));
 
end
Workbook.SaveAs(filepath);
invoke(Excel, 'Quit');
delete(Excel);


